<template>
  <div class="paperClass">
    <!-- 顶部搜索重置新增模块 -->
    <el-form size="small" :inline="true" label-width="68px">
      <el-form-item label="试题名称">
        <el-input
          placeholder="请输入试题名称"
          v-model="select"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini">重置</el-button>
        <el-button type="success" icon="el-icon-edit" size="mini" @click="dialogFormVisible = true">生成试题</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格模块 -->
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="ID"
        width="100">
      </el-table-column>
      <el-table-column
        prop="name"
        label="试题名称"
        width="160">
      </el-table-column>
      <el-table-column
        prop="single"
        label="单选题数量"
        width="160">
      </el-table-column>
      <el-table-column
        prop="multiple"
        label="多选题数量"
        width="160">
      </el-table-column>
      <el-table-column
        prop="short"
        label="简单题数量"
        width="160">
      </el-table-column>
      <el-table-column
        prop="difficulty"
        label="难易程度"
        width="160">
        <template slot-scope="scope">{{difficultName[scope.row.difficulty]}}</template>
      </el-table-column>
      <el-table-column
      label="操作">
      <template slot-scope="scope">
        <el-button @click="handleClick(scope.row)" type="success" size="small">导出为Excel</el-button>
        <el-button type="primary" size="small">删除</el-button>
      </template>
    </el-table-column>
    </el-table>
    <!-- 分页模块 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 上传文件模块 -->
    <el-dialog title="上传文档" :visible.sync="dialogFormVisible" width="30%" center>
      <!-- 内层dialog模块 -->
      <el-dialog
        width="30%"
        title="生成试题"
        :visible.sync="innerVisible"
        append-to-body center>
        <!-- 内层表单 -->
        <el-form :model="form" :rules="rules" ref="form" status-icon>

          <el-form-item label="试题名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.name" autocomplete="off" style="width: 220px;"></el-input>
          </el-form-item>
          <el-form-item label="单选题数量" :label-width="formLabelWidth" prop="single">
            <!-- <el-input v-model="form.single" autocomplete="off" style="width: 220px;"></el-input> -->
            <el-input-number v-model="form.single" :min="1" :max="+dataList.single" label="描述文字"></el-input-number>
          </el-form-item>
          <el-form-item label="多选题数量" :label-width="formLabelWidth" prop="multiple">
            <!-- <el-input v-model="form.multiple" autocomplete="off" style="width: 220px;"></el-input> -->
            <el-input-number v-model="form.multiple" :min="1" :max="+dataList.multiple" label="描述文字"></el-input-number>
          </el-form-item>
          <el-form-item label="简答题数量" :label-width="formLabelWidth" prop="short">
            <!-- <el-input v-model="form.short" autocomplete="off" style="width: 220px;"></el-input> -->
            <el-input-number v-model="form.short" :min="1" :max="+dataList.short" label="描述文字"></el-input-number>
          </el-form-item>
          <el-form-item label="难易程度" :label-width="formLabelWidth" prop="difficulty">
            <el-select v-model="form.difficulty" placeholder="试题难度">
              <el-option label="简单" value="0"></el-option>
              <el-option label="中等" value="1"></el-option>
              <el-option label="困难" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submit" v-loading.fullscreen.lock="sumLoading">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 外层上传 -->
      <el-upload
        class="upload-demo"
        ref="upload"
        drag
        :file-list="fileList"
        :auto-upload="false"
        :limit="1"
        :on-exceed="handleExceed"
        :before-upload="beforeAvatarUpload"
        style="text-align: center;"
        action="https://jsonplaceholder.typicode.com/posts/"
        multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传.docx文件，且不超过20MB</div>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button @click="uploadCancel">取 消</el-button>
        <el-button type="primary" @click="submitUpload" v-loading.fullscreen.lock="fullscreenLoading">确 定</el-button>
      </span>
      <!-- <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button> -->
    </el-dialog>
    <!-- 弹出框模块 -->
    <el-dialog title="生成试题" width="30%" center>
      <div style="text-align: center;">
        <el-form :model="form" :rules="rules" ref="form" status-icon>

          <el-form-item label="试题名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.name" autocomplete="off" style="width: 220px;"></el-input>
          </el-form-item>
          <el-form-item label="单选题数量" :label-width="formLabelWidth" prop="single">
            <el-input v-model="form.single" autocomplete="off" style="width: 220px;"></el-input>
          </el-form-item>
          <el-form-item label="多选题数量" :label-width="formLabelWidth" prop="multiple">
            <el-input v-model="form.multiple" autocomplete="off" style="width: 220px;"></el-input>
          </el-form-item>
          <el-form-item label="简答题数量" :label-width="formLabelWidth" prop="short">
            <el-input v-model="form.short" autocomplete="off" style="width: 220px;"></el-input>
          </el-form-item>
          <el-form-item label="难易程度" :label-width="formLabelWidth" prop="difficulty">
            <el-select v-model="form.difficulty" placeholder="试题难度">
              <el-option label="简单" value="0"></el-option>
              <el-option label="中等" value="1"></el-option>
              <el-option label="困难" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listTest_paper} from '@/api/generateTest/test_paper'
export default{
  data() {
    // 验证数量
    var checkNum = (rule, value, callback) => {
        value = Number.parseInt(value)
        if (!value || !Number.isInteger(value)) {
          return callback(new Error('数量不能为空并且为数值型'));
        }else {
            if (value > 20) {
              console.log(value);
              callback(new Error('数值过大!'));
            } else {
              callback();
            }
        }
      };
    return{
      select:'',
      // 文件列表
      fileList:[],
      // 测试数据
      tableData:[
        {id:1,name:'测试',single:1,multiple:1,short:1,difficulty:1},
      ],
      // 获得数据
      dataList:{id:2,name:'',single:'10',multiple:'15',short:'10',difficulty:''},
      // 外出弹窗
      dialogFormVisible:false,
      // 内层弹窗
      innerVisible:false,
      // 获得题目数量加载动画
      fullscreenLoading: false,
      // 获得excel表单加载动画
      sumLoading:false,
      // 选择表单
      form: {
        id: '',
        name: '',
        single: '',
        multiple: '',
        short: '',
        difficulty: ''
      },
      filePath: "",
      // 总条数
      total: 10,
      // 当前生成试题查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        belongingQuestionBank: null,
        testFile: null
      },
      // 难度选项
      difficultName:['简单','中等','困难'],
      formLabelWidth:"100px",
      // 匹配规则
      rules:{
        name:[{required: true, message: '试题名称', trigger: 'blur'}],
        single:[{validator:checkNum, trigger: 'blur'}],
        multiple:[{validator:checkNum, trigger: 'blur'}],
        short:[{validator:checkNum, trigger: 'blur'}],
        difficulty:[{required: true, message: '请选择难易程度', trigger: 'blur'}]
      }
    }
  },
  methods:{
    handleClick(row) {
      console.log(row);
    },
    // 取消并且重置表单
    cancel(){
      this.innerVisible = false;
      this.$refs['form'].resetFields();
    },
    // 上传文件弹窗取消按钮
    uploadCancel(){
      this.dialogFormVisible = false;
      this.fileList = []
    },
    submit(){

      this.$refs['form'].validate((valid) => {
          if (valid) {
            this.fullscreenLoading = true;
            setTimeout(() => {
              this.tableData.push(this.form)
              this.innerVisible = false;
              
              this.fullscreenLoading = false;
            }, 2000);
            // console.log(this.form);
            // console.log(this.tableData);
          } else {
            // console.log('error submit!!');
            return false;
          }
        });
    },
    // 上传文件提交按钮
    submitUpload(){
      this.dialogFormVisible = false;
      // this.$refs.upload.submit();
      // 开启加载动画
      this.fullscreenLoading = true;
      // 模拟上传成功，接受到数据关闭动画显示内层弹窗
      setTimeout(() => {
        this.fullscreenLoading = false;
        this.innerVisible = true
        this.form = JSON.parse(JSON.stringify(this.dataList))
      }, 500);

    },
    getList(){
      console.log(111);
    },
    handleExceed(files, fileList){
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    // 文件上传
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
      console.log(file.type);
      const isLt2M = file.size / 1024 / 1024 < 20;

      if (!isJPG) {
        this.$message.error('上传文件只能是 DOCX/DOC 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 20MB!');
      }
      return isJPG && isLt2M;
    }
  },
  mounted(){
    
  }
}
</script>

<style scoped lang="scss">
.paperClass{
  margin: 20px 20px;
}
</style>
